package com.datastructure.test.findpeek;

public class FindPeek {

    public static void main(String[] args) {

    }

    public int findPeakElement (int[] nums) {
        // write code here
        if(nums.length==0){
            return 0;
        }
        if(nums.length==2){
            if(nums[1]>nums[0]){
                return 1;
            }else {
                return 0;
            }
        }
        int left = 0;
        int right = 0;
        int index = 0;
        boolean flag = false;
        for (int i = 1; i < nums.length-1; i++) {
            left = nums[i-1];
            right = nums[i+1];
            if(nums[i]>left&&nums[i]>right){
                flag=true;
                index=i;
                break;
            }
        }
        if(!flag){
            if(nums.length>=2) {
                if (nums[1] > nums[0]) {
                    return nums.length - 1;
                } else {
                    return 0;
                }
            }
        }
        return index;
    }
}
